import prisma from './prisma.service';
import { Group, GroupRole } from '@prisma/client';

class GroupService {
    async createGroup(data: {
        groupId: string;
        name: string;
        creatorId: string;
        description?: string;
    }) {
        return prisma.group.create({ data });
    }

    disbandGroup(groupId: string) {
        return prisma.group.update({
            where: { groupId }, data: { status: 'DISBAND' }
        })
    }

    updateGroup(groupId: string, name?: string, description?: string) {
        return prisma.group.update({
            where: { groupId },
            data: {
                name,
                description,
            }
        })
    }

    getGroup(groupId: string) {
        return prisma.group.findUnique({
            where: { groupId },
            include: {
                members: {
                    include: {
                        user: {
                            select: {
                                id: true,
                                userId: true,
                                nickname: true
                            }
                        }
                    }
                }
            }
        });
    }

    createGroupMember(groupId: string, userId: string, role: GroupRole = 'MEMBER') {
        return prisma.groupUser.create({
            data: {
                groupId,
                userId,
                role
            },
            include: {
                group: true,
                user: true
            }
        });
    }

    removeGroupMember(groupId: string, userId: string) {
        return prisma.groupUser.delete({
            where: {
                groupId_userId: {
                    groupId,
                    userId
                }
            }
        });
    }

    updateMemberRole(groupId: string, userId: string, role: GroupRole) {
        return prisma.groupUser.update({
            where: {
                groupId_userId: {
                    groupId,
                    userId
                }
            },
            data: { role }
        });
    }

    getGroupUser(groupId: string, userId: string) {
        return prisma.groupUser.findUnique({
            where: {
                groupId_userId: {
                    groupId,
                    userId
                }
            },
            include: {
                group: true,
                user: true
            }
        })
    }

    getGroupMemberCount(groupId: string) {
        return prisma.groupUser.count({
            where: { groupId }
        });
    }


    getUserGroups(userId: string) {
        return prisma.groupUser.findMany({
            where: { userId },
            include: { group: true }
        });
    }

    getGroupMembers(groupId: string) {
        return prisma.groupUser.findMany({
            where: { groupId },
            include: {
                user: {
                    select: {
                        id: true,
                        nickname: true
                    }
                }
            }
        })
    }

    getGroupAdmins(groupId: string) {
        return prisma.groupUser.findMany({
            where: {
                groupId, role: { in: ['OWNER', 'ADMIN'] }
            },
        })
    }
}

const groupService = new GroupService();
export default groupService;
